{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-23T13:22:45.002875Z",
     "start_time": "2025-01-23T13:22:39.077553Z"
    }
   },
   "source": [
    "#导入cv模块\n",
    "import cv2 as cv\n",
    "\n",
    "def face_detect_demo():\n",
    "    gary=cv.cvtColor(img,cv.COLOR_BGR2GRAY)#转为灰度图\n",
    "    face_detect=cv.CascadeClassifier(r'D:\\ProgramData\\opencv4.5.2\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml')#加载分类器\n",
    "    face=face_detect.detectMultiScale(gary,1.01,5,0,(100,100),(300,300))#参数：图像，缩放倍数，检测几次确定，0,最小和最大像素框\n",
    "    for (x,y,w,h) in face:\n",
    "        cv.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=2)\n",
    "        cv.imshow('result',img)\n",
    "\n",
    "#读取图片\n",
    "img = cv.imread('zidane.jpg')\n",
    "\n",
    "#检测函数\n",
    "face_detect_demo()\n",
    "\n",
    "#等待 0是无限等待下去，waitkey（）参数是等待的时间\n",
    "while True:\n",
    "    if ord('q') == cv.waitKey(0):#检测到q退出\n",
    "        break\n",
    "\n",
    "#释放内存\n",
    "cv.destroyAllWindows()"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "e55ef7f8479105b6",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "718ffcb8616e123",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "c9542d12919f8409"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
